home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / dnet / dnetsolaris.lzh / server / sgcopy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-12  |  5.3 KB  |  326 lines

  1.  
  2. /*
  3.  *  SGCOPY.C     V1.1
  4.  *
  5.  *  DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  GET-COPY SERVER    (NEW COPY SERVER)
  8.  *
  9.  *  The current version only accepts one connection at a time.    This server
  10.  *  will send requested files to the remote machine.
  11.  *
  12.  *  length in 68000 longword format.
  13.  */
  14.  
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/wait.h>
  18. #include <sys/time.h>
  19. #include <sys/dir.h>
  20. #include <sys/file.h>
  21. #include <sys/resource.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <signal.h>
  25.  
  26. #include "servers.h"
  27.  
  28. typedef struct {
  29.     char    Cmd;
  30.     char    Str[64];
  31.     long    Val;
  32. } HDR;
  33.  
  34. typedef unsigned char ubyte;
  35. char *getnamepart();
  36. char *getdirpart();
  37.  
  38. char Buf[4096];
  39. int Chan;
  40.  
  41. chandler()
  42. {
  43.     union wait stat;
  44.     struct rusage rus;
  45.     while (wait3(&stat, WNOHANG, &rus) > 0);
  46. }
  47.  
  48. main(ac,av)
  49. char *av[];
  50. {
  51.     long chann = DListen(PORT_GFILECOPY);
  52.     int fd;
  53.     int n;
  54.     char buf[256];
  55.     extern int errno;
  56.  
  57.     if (av[1])
  58.     chdir(av[1]);
  59.     signal(SIGCHLD, chandler);
  60.     signal(SIGPIPE, SIG_IGN);
  61.     for (;;) {
  62.     fd = DAccept(chann);
  63.     if (fd < 0)
  64.         break;
  65.     if (fork() == NULL) {
  66.         SGCopy(fd);
  67.         _exit(1);
  68.     }
  69.     close(fd);
  70.     }
  71. }
  72.  
  73. SGCopy(fd)
  74. int fd;
  75. {
  76.     short error = 0;
  77.     static HDR Hdr;
  78.  
  79.     Chan = fd;
  80.     error = WriteHeader('H', "Hello, GCopy server V1.30", 0);
  81.     if (error)
  82.     return(error);
  83.     switch(ReadHeader(&Hdr)) {
  84.     default:
  85.     case -1:
  86.     error = 1;
  87.     return(error);
  88.     case 'H':
  89.     break;
  90.     }
  91.     while (!error) {
  92.     switch(ReadHeader(&Hdr)) {
  93.     case 'G':
  94.         {
  95.         char svdir[1024];
  96.         getwd(svdir);
  97.         if (chdir(getdirpart(Hdr.Str)) < 0) {
  98.             error = WriteHeader('N', "Unable to cd to dir", 0);
  99.         } else {
  100.                 error = PutObject(getnamepart(Hdr.Str));
  101.         }
  102.         chdir(svdir);
  103.         }
  104.         break;
  105.     case 'E':
  106.         goto done;
  107.     case 'P':   /*  put-files, not implemented  */
  108.     default:
  109.         error = 1;
  110.         break;
  111.     }
  112.     }
  113. done:
  114.     ;
  115. }
  116.  
  117. PutObject(str)
  118. char *str;
  119. {
  120.     struct stat stat;
  121.     short error = 0;
  122.  
  123.     if (lstat(str, &stat) < 0) {
  124.     error = WriteHeader('N', "Unable to find object", 0);
  125.     return(0);
  126.     }
  127.     if (stat.st_mode & S_IFDIR) {
  128.     error = PutDir(str);
  129.     } else {
  130.     error = PutFile(str);
  131.     }
  132.     return(0);
  133. }
  134.  
  135. PutDir(name)
  136. char *name;
  137. {
  138.     struct stat stat;
  139.     char svdir[1024];
  140.     static HDR Hdr;
  141.     short error = 0;
  142.     char *fn = getnamepart(name);
  143.     DIR *dir;
  144.     struct direct *de;
  145.  
  146.     if (lstat(name, &stat) < 0 || !(dir = opendir(name))) {
  147.     WriteHeader('N', "Possible Disk Error", 0);
  148.     error = 1;
  149.     goto done;
  150.     }
  151.     if (error = WriteHeader('D', fn, 0)) 
  152.     goto done;
  153.     switch(ReadHeader(&Hdr)) {
  154.     case 'Y':
  155.     break;
  156.     case 'S':
  157.     goto done;
  158.     case 'N':
  159.     error = 1;
  160.     break;
  161.     default:
  162.     error = 1;
  163.     break;
  164.     }
  165.     if (error)
  166.     goto done;
  167.  
  168.     getwd(svdir);
  169.     if (chdir(name) < 0) {
  170.     error = 1;
  171.     WriteHeader('N', "unable to chdir", 0);
  172.     }
  173.     if (error)
  174.     goto done;
  175.  
  176.     while (de = readdir(dir)) {
  177.     if (strcmp(de->d_name, ".") == 0)
  178.         continue;
  179.     if (strcmp(de->d_name, "..") == 0)
  180.         continue;
  181.     if (lstat(de->d_name, &stat) < 0) {
  182.         continue;
  183.     }
  184.     if (stat.st_mode & S_IFDIR) {
  185.         error = PutDir(de->d_name);
  186.     } else {
  187.         error = PutFile(de->d_name);
  188.     }
  189.     if (error)
  190.         break;
  191.     }
  192.     WriteHeader('E', NULL, 0);
  193.     chdir(svdir);
  194. done:
  195.     return(error);
  196. }
  197.  
  198. PutFile(name)
  199. char *name;
  200. {
  201.     int fd = -1;
  202.     static HDR Hdr;
  203.     long len;
  204.     short error = 0;
  205.     char *fn = getnamepart(name);
  206.  
  207.     fd = open(fn, O_RDONLY, 0);
  208.     if (fd < 0) {       /*  don't do anything if unable to open it */
  209.     WriteHeader('N', "file not readable", 0);
  210.     goto done;
  211.     }
  212.     len = lseek(fd, 0L, 2);
  213.     if (error = WriteHeader('F', fn, len))
  214.     goto done;
  215.     switch(ReadHeader(&Hdr)) {
  216.     case 'Y':
  217.     lseek(fd, Hdr.Val, 0);  /*  start pos.  */
  218.     len -= Hdr.Val;
  219.     if (len < 0)
  220.         len = 0;
  221.     break;
  222.     case 'S':
  223.     goto done;
  224.     case 'N':
  225.     error = 1;
  226.     break;
  227.     default:
  228.     error = 1;
  229.     break;
  230.     }
  231.     if (error)
  232.     goto done;
  233.     while (len) {
  234.     register long n = (len > sizeof(Buf)) ? sizeof(Buf) : len;
  235.  
  236.     if (read(fd, Buf, n) != n) {    /*  read failed! */
  237.         error = 10;
  238.         goto done;
  239.     }
  240.     if (gwrite(Chan, Buf, n) != n) {
  241.         error = 10;
  242.         goto done;
  243.     }
  244.     len -= n;
  245.     }
  246. done:
  247.     if (fd >= 0)
  248.     close(fd);
  249.     return(error);
  250. }
  251.  
  252.  
  253. WriteHeader(c, str, len)
  254. char c;
  255. char *str;
  256. long len;
  257. {
  258.     ubyte sl;
  259.  
  260.     if (str == NULL)
  261.     str = "";
  262.     sl = strlen(str);
  263.  
  264.     if (gwrite(Chan, &c, 1) < 0)
  265.     return(1);
  266.     if (gwrite(Chan, &sl,1) < 0)
  267.     return(1);
  268.     if (gwrite(Chan, str, sl) != sl)
  269.     return(1);
  270.     len = htonl68(len);
  271.     if (gwrite(Chan, &len, 4) != 4)
  272.     return(1);
  273.     return(0);
  274. }
  275.  
  276. ReadHeader(hdr)
  277. HDR *hdr;
  278. {
  279.     ubyte sl;
  280.     ubyte cmd;
  281.  
  282.     hdr->Cmd = -1;
  283.     if (ggread(Chan, &cmd, 1) != 1)
  284.     return(-1);
  285.     if (ggread(Chan, &sl, 1) != 1)
  286.     return(-1);
  287.     if (sl >= sizeof(hdr->Str)) {
  288.     return(-1);
  289.     }
  290.     if (ggread(Chan, hdr->Str, sl) != sl)
  291.     return(-1);
  292.     hdr->Str[sl] = 0;
  293.     if (ggread(Chan, &hdr->Val, 4) != 4)
  294.     return(-1);
  295.     hdr->Val = ntohl68(hdr->Val);
  296.     hdr->Cmd = cmd;
  297.     return(hdr->Cmd);
  298. }
  299.  
  300. char *
  301. getnamepart(str)
  302. char *str;
  303. {
  304.     register char *ptr = str + strlen(str);
  305.  
  306.     while (ptr >= str) {
  307.     if (*ptr == '/')
  308.         break;
  309.     --ptr;
  310.     }
  311.     return(ptr+1);
  312. }
  313.  
  314. char *
  315. getdirpart(str)
  316. char *str;
  317. {
  318.     static char buf[1024];
  319.  
  320.     strcpy(buf, str);
  321.     getnamepart(buf)[0] = 0;
  322.     if (buf[0] == 0)
  323.     return(".");
  324.     return(buf);
  325. }
  326.